//
//  SupplementCell.m
//  MyPizzaMobile
//
//  Created by Anton Atanasov on 16.02.13.
//  Copyright (c) 2013 г. Stanislav. All rights reserved.
//

#import "SupplementCell.h"

@interface SupplementCell()
@property (readwrite) RequestManager* requestManager;
@end
@implementation SupplementCell
@synthesize tableViewIndex, amountOrdered, ppu, identity, amount, unitType;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)init
{
    self = [super init];
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)amountValueChange:(UIStepper *)sender
{
    NSMutableDictionary *supplement = [[NSMutableDictionary alloc]init];
    [supplement setObject:[[NSString alloc]initWithFormat:@"%@",self.tableViewIndex] forKey:@"id"];
    [supplement setObject:[[NSString alloc]initWithFormat:@"%f",sender.value] forKey:@"amountOrdered"];
    [supplement setObject:[[NSString alloc]initWithFormat:@"%f",(self.ppu.doubleValue*sender.value)] forKey:@"total"];
    self.requestManager = (self.requestManager?self.requestManager:[RequestManager requestManagerInstance]);
    [self.requestManager.orderSupplements setObject:supplement forKey:[[NSString alloc]initWithFormat:@"%@",self.tableViewIndex]];
    if(sender.value==0.0f)
    {
        [self.requestManager.orderSupplements removeObjectForKey:[[NSString alloc]initWithFormat:@"%@",self.tableViewIndex]];
        
    }
    [self refreshData];
    
    
}
- (void)refreshData
{
    self.requestManager = (self.requestManager?self.requestManager:[RequestManager requestManagerInstance]);
    if(self.requestManager.orderSupplements &&
       [self.requestManager.orderSupplements objectForKey:[[NSString alloc]initWithFormat:@"%@",self.tableViewIndex]])
    {
        NSDictionary * supplement = (NSDictionary *)[self.requestManager.orderSupplements objectForKey:[[NSString alloc]initWithFormat:@"%@",self.tableViewIndex]];
        self.amountOrdered = [supplement objectForKey:@"amountOrdered"];
        if(self.amountOrdered>0)
        {
            self.amountLabel.text = [[NSString alloc]initWithFormat:@"%.0f %@",(self.amountOrdered.intValue*self.amount.doubleValue),self.unitType];
            self.totalLabel.text = [[NSString alloc]initWithFormat:@"$ %.2f",((self.amountOrdered.intValue)*self.ppu.doubleValue)];
        }
    }
}

@end
